package com;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * ClassName:SpringSourceCodeApplication
 * Package:com
 *
 * @Date:2024/7/18 14:01
 * @Author:zxde
 */
@SpringBootApplication
public class SpringSourceCodeApplication {
    public static void main(String[] args)  {

        //SpringBootApplication ,SpringApplication
        ConfigurableApplicationContext context = SpringApplication.run(SpringSourceCodeApplication.class);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();

        Class<DefaultSingletonBeanRegistry> defaultSingletonBeanRegistryClass = DefaultSingletonBeanRegistry.class;
        try {
            Field singletonObjects = defaultSingletonBeanRegistryClass.getDeclaredField("singletonObjects");

            singletonObjects.setAccessible(true);

            Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);

            map.entrySet().stream().filter(e -> e.getKey().startsWith("component"))
                    .forEach(e -> System.out.println(e.getKey() + "=" + e.getValue()));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            context.close();
        }

    }

}
